package com.gexiaobao.pigeon.ui.fragment.mine.organization

import android.os.Bundle
import android.view.View
import android.view.inputmethod.EditorInfo
import androidx.recyclerview.widget.StaggeredGridLayoutManager
import com.gexiaobao.pigeon.R
import com.gexiaobao.pigeon.app.base.BaseFragment
import com.gexiaobao.pigeon.app.ext.hideSoftKeyboard
import com.gexiaobao.pigeon.app.ext.init
import com.gexiaobao.pigeon.app.ext.initClose
import com.gexiaobao.pigeon.app.ext.loadListData
import com.gexiaobao.pigeon.app.ext.loadServiceInit
import com.gexiaobao.pigeon.app.ext.showLoading
import com.gexiaobao.pigeon.app.util.RxToast
import com.gexiaobao.pigeon.databinding.FragmentSearchOrganizationBinding
import com.gexiaobao.pigeon.ui.adapter.OrganizationAdapter
import com.gexiaobao.pigeon.viewmodel.OrganizationViewModel
import com.kingja.loadsir.core.LoadService
import me.hgj.jetpackmvvm.ext.nav
import me.hgj.jetpackmvvm.ext.navigateAction
import me.hgj.jetpackmvvm.ext.util.setOnclickNoRepeat

/**
 * @Author      : hxw
 * @Date        : 2023/5/31 15:57
 * @Describe    : 搜索组织
 */
class FragmentSearchOrg : BaseFragment<OrganizationViewModel, FragmentSearchOrganizationBinding>() {

    private val mAdapter: OrganizationAdapter by lazy { OrganizationAdapter(arrayListOf()) }
    private var mOrgId = 0

    //界面状态管理者
    private lateinit var loadsir: LoadService<Any>
    private var mStatus = 0

    override fun initView(savedInstanceState: Bundle?) {
        mDatabind.viewmodel = mViewModel
        mStatus = requireArguments().getInt("status")

        mDatabind.etSearch.setOnEditorActionListener { _, i, _ ->
            when (i) {
                EditorInfo.IME_ACTION_SEARCH ->
                    searchOrganization()

                else -> {}
            }
            false
        }

        val layoutManager = StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL)
        mDatabind.rvSearchOrganization.init(layoutManager, mAdapter, false)

        //状态页配置
        loadsir = loadServiceInit(mDatabind.listSmartRefresh) {
            //点击重试时触发的操作
            loadsir.showLoading()
            mViewModel.getOrganizationCallBack(true, mViewModel.searchname.get(), mStatus)
        }
        val title = requireArguments().getString("title")
        mDatabind.includeBar.toolbar.run {
            initClose(getString(R.string.activity_market_shop_search_result_search) + title) {
                nav().navigateUp()
            }
        }

        mAdapter.run {
            setOnItemClickListener { _, _, position ->
                if (mStatus != 1 && mStatus != 2) {
                    mOrgId = mAdapter.data[position].id
                    nav().navigateAction(R.id.action_to_org_info, Bundle().apply {
                        putInt("orgId", mOrgId)
                        putString("type", "org")
                    })
                } else {
                    nav().navigateAction(R.id.action_search_to_org_detail, Bundle().apply {
                        putParcelable("data", mAdapter.data[position])
                    })
                }
            }
        }
        mDatabind.listSmartRefresh.init {
            if (mViewModel.searchname.get().isEmpty()) {
                RxToast.showToast(getString(R.string.input_search_content))
                mDatabind.listSmartRefresh.isRefreshing = false
                return@init
            }
            //触发刷新监听时请求数据
            mViewModel.getOrganizationCallBack(true, mViewModel.searchname.get(), mStatus)
        }
    }

    private fun searchOrganization() {
        loadsir.showLoading()
        mViewModel.getOrganizationCallBack(true, mViewModel.searchname.get(), mStatus)
    }

    override fun onBindViewClick() {
        super.onBindViewClick()
        setOnclickNoRepeat(mDatabind.tvSearch) {
            when (it) {
                mDatabind.tvSearch -> {
                    hideSoftKeyboard(activity)
                    loadsir.showLoading()
                    mViewModel.getOrganizationCallBack(true, mViewModel.searchname.get(), mStatus)
                }
            }
        }
    }

    override fun createObserver() {
        super.createObserver()
        mViewModel.searchOrgData.observe(this) {
            dismissLoading()
            if (mDatabind.listSmartRefresh.isRefreshing) {
                mDatabind.listSmartRefresh.isRefreshing = false
            }
            mDatabind.organizeSearchNumber.visibility = View.VISIBLE
            if (it.isEmpty) {
                mViewModel.searchResult.set(getString(R.string.search_zero_pigeon))
            } else {
                mViewModel.searchResult.set(getString(R.string.look_up) + it.total + getString(R.string.pipeidezuzhi))
            }
            loadListData(it, mAdapter, loadsir, mDatabind.rvSearchOrganization, mDatabind.listSmartRefresh)
        }
    }
}